import Cookies from "js-cookie";

// let map = new Map();
// let mapInner = new Map();
// mapInner.set("1","abc");
// mapInner.set("2","kil");
// map.set("gender",mapInner);
// let mapInner2 = new Map();
// mapInner2.set("1","abcefg");
// mapInner2.set("2","kilkkk");
// map.set("status",mapInner2);

// function myFunction2() {
//     document.getElementById("demo").innerHTML = ages.every(checkAdult);
// }
// function myFunction() {
//     document.getElementById("demo").innerHTML = map.get("gender").get("3");
// }

const app = {
    state: {
        panels: [],
        sidebar: {
            opened: !+Cookies.get("sidebarStatus"),
            withoutAnimation: false,
            mainTabs: {
                editableTabsValue: "1",
                editableTabs: [
                    {
                        title: "首页",
                        name: "1",
                        panelComponent: () => import("@/panels/MandyHome")
                    }
                ],
                tabIndex: 1
            }
        },
        device: "desktop",
        mainMenu: [
            {
                componentName: "MandyTest1",
                iconClass: "el-icon-s-home",
                id: 412,
                parentId: 0,
                title: "HomeXXXX",
                children: []
            },
            {
                children: [
                    {
                        componentName: "MandyPanel",
                        iconClass: "el-icon-folder",
                        id: 416,
                        parentId: 415,
                        title: "面板功能",
                        children: []
                    },
                    {
                        componentName: "MandyUsers",
                        iconClass: "el-icon-user-solid",
                        id: 421,
                        parentId: 415,
                        title: "用户管理",
                        children: []
                    },
                    {
                        componentName: "MandyRole",
                        iconClass: "el-icon-user",
                        id: 432,
                        parentId: 415,
                        title: "角色管理",
                        children: []
                    }
                ],
                id: 415,
                parentId: 0,
                title: "系统管理",
                iconClass: "el-icon-s-tools"
            }
        ]
    },
    mutations: {
        TOGGLE_SIDEBAR: state => {
            if (state.sidebar.opened) {
                Cookies.set("sidebarStatus", 1);
            } else {
                Cookies.set("sidebarStatus", 0);
            }
            state.sidebar.opened = !state.sidebar.opened;
        },
        CLOSE_SIDEBAR: (state, withoutAnimation) => {
            Cookies.set("sidebarStatus", 1);
            state.sidebar.opened = false;
            state.sidebar.withoutAnimation = withoutAnimation;
        },
        TOGGLE_DEVICE: (state, device) => {
            state.device = device;
        },
        INIT_PANELS: (state, panels) => {
            state.panels = panels;
        },
        // item.componentName + "+" + item.id + "+" + item.title;
        ADD_MAIN_TAB: (state, key) => {
            let realkey = key.split("+")[0];
            let title = key.split("+")[2];
            let newPanel = state.panels[realkey];
            if (newPanel) {
                let newTabName = ++state.sidebar.mainTabs.tabIndex + "";
                state.sidebar.mainTabs.editableTabs.push({
                    title: title,
                    name: newTabName,
                    panelComponent: newPanel
                });
                state.sidebar.mainTabs.editableTabsValue = newTabName;
            }
        },
        REMOVE_MAIN_TAB: (state, targetName) => {
            if (targetName === "1") {
                return;
            }
            let tabs = state.sidebar.mainTabs.editableTabs;
            let activeName = state.sidebar.mainTabs.editableTabsValue;
            if (activeName === targetName) {
                tabs.forEach((tab, index) => {
                    if (tab.name === targetName) {
                        let nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                        }
                    }
                });
            }

            state.sidebar.mainTabs.editableTabsValue = activeName;
            state.sidebar.mainTabs.editableTabs = tabs.filter(
                tab => tab.name !== targetName
            );
        }
    },
    actions: {
        ToggleSideBar: ({ commit }) => {
            commit("TOGGLE_SIDEBAR");
        },

        CloseSideBar({ commit }, { withoutAnimation }) {
            commit("CLOSE_SIDEBAR", withoutAnimation);
        },
        ToggleDevice({ commit }, device) {
            commit("TOGGLE_DEVICE", device);
        },
        InitPanels({ commit }, panels) {
            commit("INIT_PANELS", panels);
        },
        AddMainTab({ commit }, { key }) {
            commit("ADD_MAIN_TAB", key);
        },
        RemoveMainTab({ commit }, { targetName }) {
            commit("REMOVE_MAIN_TAB", targetName);
        }
    }
};

export default app;
